/* UTF8 Encoding */
/*-----------------------------------------------------------------------
|                            FILE DESCRIPTION                           |
-----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
  - File name     : wifi_connect.hpp
  - Author        : liuzhihua (liuzhihuawy@163.com)
  - Update date   : 2024.04.13
  - Brief         : wifi connnect class
  - Version       : v0.1
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               UPDATE NOTE                             |
-----------------------------------------------------------------------*/
/**
  * Update note:
  * ------------   ---------------   ----------------------------------
  *     Date            Author                      Note
  * ------------   ---------------   ----------------------------------
  *  2024.05.28       liuzhihua                  Create file          
***/

#ifndef WIFI_CONNECT_HPP
#define WIFI_CONNECT_HPP

/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include <stdint.h>
#include "at_aithinker_combo.h"
#include "strutil.hpp"

/*-----------------------------------------------------------------------
|                                DEFINES                                |
-----------------------------------------------------------------------*/
class wifiConnect
{
    public:
        typedef enum {
            STATE_IDLE = 0,
            //初始化
            STATE_INIT,
            //尝试连接
            STATE_TRY_CONNECT,
            //尝试订阅
            STATE_TRY_SUBSCRIBE,
            //连接完成
            STATE_CPLT,
            //停止配网
            STATE_STOP
        }wifi_connect_state_e;

    private:

        bool sleep(uint32_t ms, uint8_t subState);
        bool sleep(uint32_t ms);
        bool awake(void);
        bool awake(uint8_t subState);
        static void wake_task(const void * arg);
        //void run_connect(void);

    public:
        AT_AiCombo *wifi;
        // 状态机当前状态
        wifi_connect_state_e state;
        // 子状态，休眠完成后赋值
        uint8_t sub_state;
        uint8_t sub_state_shadow;
        // 状态时间
        uint32_t time;
        // 超时时间
        uint32_t timeout;
        // 休眠时间
        uint32_t sleep_time;
        // 配网WIFI SSID
        const char *ap_ssid; //64 byte
        // 配网WIFI密码
        const char *ap_pwd;  //64 byte
        // 配网WIFI Mac eg["de:3a:45:72:89:37"]
        const char *ap_bssid; //18

        wifiConnect(AT_AiCombo *wifiModule);
        // ~wifiConnect();
        void start_connect(void);
        void start_connect(const char *ssid, const char *password);
        void start_connect(const char *ssid, const char *password, const char *bssid);
        void stop_connect(void);
        int setAP(const char *ssid, const char *pwd);
        int setAP(const char *ssid, const char *pwd, const char *bssid);
        void setTimeout(uint32_t time);
        bool get_state(void);
        void run_connect(void);
};

extern wifiConnect wifi;
extern AT_AiCombo::mqtt_cfg_t user_mqtt_cfg;
/*-----------------------------------------------------------------------
|                                  API                                  |
-----------------------------------------------------------------------*/


#ifdef  __cplusplus
    extern "C" {
#endif
void user_subscribe_callback(uint16_t id, const uint8_t *pPayload, uint16_t len);
#ifdef __cplusplus
	}
#endif
#endif
